<?php if (!defined('BASEPATH')) exit ('No direct script access allowed');

class Level extends CI_Controller {
	function __construct(){
		parent::__construct();
		$this->load->library('encrypt');
		$this->load->model('user_model');
		$this->load->model('question_model');
		$this->load->model('level_model');
		$this->load->library('form_validation');
		$this->load->library('email');
		
		if(!isset($this->session->userdata['adminflag'])){
			redirect('/');
		}
	}
	
	public function admin_add(){
		if($this->input->post()){
			$level = $this->getLevel();
			if($level){
				$level = $this->level_model->get_level_byname($level['level_name']);
				if(!empty($level)){
					$data['message'] = "名称已存在，添加失败！";
				}else{
					$this->level_model->level_insert($level);
					$data['message'] = "添加成功！";
					$this->admin_list($data);
					return ;
				}
			}
		}
		$data['title'] = 'Add Level';
		$this->load->view('templates/header', $data);
		$this->load->view('level/add', $data);
		$this->load->view('templates/footer');
	}
	
	private function getLevel(){
		$this->form_validation->set_rules('l_name', 'Name', 'trim|required');
		$this->form_validation->set_rules('l_desc', 'Description', 'trim|required');
		if($this->form_validation->run() == TRUE){
	
			$level["level_name"] = $this->input->post("l_name");
			$level["description"] = $this->input->post("l_desc");
			return $level;
		}
		return FALSE;
	}
	
	public function admin_edit(){
		$id = $this->uri->segment(4);
		if($this->input->post()){
			$level = $this->getLevel();
			if($level){
				$level["id"] = $id;
				$this->level_model->level_update($level["id"], $level);
				$data['message'] = "修改成功！";
			}
		}
		
		$level = $this->level_model->get_level($id);
		$data['level'] = $level;
		$data['title'] = 'Edit Level';
		$this->load->view('templates/header', $data);
		$this->load->view('level/edit', $data);
		$this->load->view('templates/footer');
	}
	
	public function admin_list($data = array()) {
		$this->load->library('pagination');
		$search = array();
		if($this->input->post()){
			$search['level_name'] = $this->input->post('level_name');
			$search['description'] = $this->input->post('description');
			$search['sort'] = $this->input->post('sort');
			$search['desc'] = $this->input->post('desc');
		}
		
		$total = $this->level_model->count_level($search);
		$config = $this->get_page_config('index.php/admin/level/list/', $total);
		$offset = $this->uri->segment(4);
		$offset = $offset?$offset:0;
		
		$data['results'] = $this->level_model->list_level($search, $config['per_page'], $offset);
		$data['offset'] = $offset;
		$data['search'] = $search;
		
		$this->pagination->initialize($config); 
		$this->load->view('templates/header', $data);
		$this->load->view('level/list', $data);
		$this->load->view('templates/footer');
		
	}
	
	private function get_page_config($uri, $total){
		$config['base_url'] = base_url().$uri;
		$config['total_rows'] = $total;
		$config['per_page'] = 5; 
		$config['num_links'] = 2; 
		$config['uri_segment'] = 4;
		
		$config['full_tag_open'] = '<ul class="pagination">';
		$config['full_tag_close'] = '</ul>';
		
		$config['first_link'] = '&lt;&lt;';
		$config['first_tag_open'] = '<li>';
		$config['first_tag_close'] = '</li>';
		
		$config['next_link'] = '&gt;';
		$config['next_tag_open'] = '<li>';
		$config['next_tag_close'] = '</li>';
		
		$config['prev_link'] = '&lt;';
		$config['prev_tag_open'] = '<li>';
		$config['prev_tag_close'] = '</li>';
		
		$config['last_link'] = '&gt;&gt;';
		$config['last_tag_open'] = '<li>';
		$config['last_tag_close'] = '</li>';
		
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		
		$config['cur_tag_open'] = '<li class="selected"><a href="#">';
		$config['cur_tag_close'] = '</a></li>';
		return $config;
	}
}
